package com.inns.position.hbase.coprocessor.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.protobuf.ByteString;
import com.google.protobuf.RpcCallback;
import com.google.protobuf.RpcController;
import com.google.protobuf.Service;
import com.inns.position.hbase.bean.StatisticsResult;
import com.inns.position.hbase.coprocessor.service.ObserverStatisticsProtos;
import com.inns.position.hbase.util.StatisticsUtil;
import org.apache.hadoop.hbase.Coprocessor;
import org.apache.hadoop.hbase.CoprocessorEnvironment;
import org.apache.hadoop.hbase.coprocessor.CoprocessorException;
import org.apache.hadoop.hbase.coprocessor.CoprocessorService;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ObserverStatisticsServiceImpl extends ObserverStatisticsProtos.StatisticsService implements CoprocessorService, Coprocessor {

    private RegionCoprocessorEnvironment env;

    @Override
    public void getStatistics331(RpcController controller, ObserverStatisticsProtos.Request request, RpcCallback<ObserverStatisticsProtos.Response> done) {
        List<StatisticsResult> resultList = StatisticsUtil.doStatistics(env, request);
//        StatisticsUtil.printResultList(resultList);
        List<ObserverStatisticsProtos.ExpandRow> resultRows = new ArrayList<ObserverStatisticsProtos.ExpandRow>();
        for (StatisticsResult map : resultList) {
            ObserverStatisticsProtos.ExpandRow.Builder builder = ObserverStatisticsProtos.ExpandRow.newBuilder();
            ObserverStatisticsProtos.ExpandCell.Builder cb = ObserverStatisticsProtos.ExpandCell.newBuilder();
            cb.setValue(ByteString.copyFromUtf8(JSON.toJSONString(map)));
            builder.addCells(cb.build());
            ObserverStatisticsProtos.NamePair.Builder newBuilder = ObserverStatisticsProtos.NamePair.newBuilder();
            newBuilder.setName("tName");
            newBuilder.setValue("tValue");
            builder.addExtends(newBuilder.build());
            resultRows.add(builder.build());
        }
        if (resultRows.size() > 0) {
            ObserverStatisticsProtos.Response.Builder responseBuilder = ObserverStatisticsProtos.Response.newBuilder();
            responseBuilder.addAllResults(resultRows);
            ObserverStatisticsProtos.Response response = responseBuilder.build();
            done.run(response);
        }
    }

    @Override
    public void getStatistics332(RpcController controller, ObserverStatisticsProtos.Request request, RpcCallback<ObserverStatisticsProtos.Response> done) {

    }

    @Override
    public void getStatistics333(RpcController controller, ObserverStatisticsProtos.Request request, RpcCallback<ObserverStatisticsProtos.Response> done) {

    }

    @Override
    public void getStatistics334(RpcController controller, ObserverStatisticsProtos.Request request, RpcCallback<ObserverStatisticsProtos.Response> done) {

    }

    @Override
    public void getStatistics333Poi(RpcController controller, ObserverStatisticsProtos.Request request, RpcCallback<ObserverStatisticsProtos.Response> done) {

    }

    @Override
    public void getStatistics335(RpcController controller, ObserverStatisticsProtos.Request request, RpcCallback<ObserverStatisticsProtos.Response> done) {

    }

    @Override
    public void getStatistics335Poi(RpcController controller, ObserverStatisticsProtos.Request request, RpcCallback<ObserverStatisticsProtos.Response> done) {

    }

    @Override
    public void getStatistics336(RpcController controller, ObserverStatisticsProtos.Request request, RpcCallback<ObserverStatisticsProtos.Response> done) {

    }

    @Override
    public void start(CoprocessorEnvironment env) throws IOException {
        if (env instanceof RegionCoprocessorEnvironment) {
            this.env = (RegionCoprocessorEnvironment) env;
        } else {
            throw new CoprocessorException("Must be loaded on a table region!");
        }
    }

    @Override
    public void stop(CoprocessorEnvironment env) throws IOException {

    }

    @Override
    public Service getService() {
        return this;
    }
}
